package emailOption;

import sqlDb.Data;
import sqlDb.emailAr;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
public class sendEM {
    public static String Text;
    public static void sendMail(String fromMail, String user, String password,
                                String toMail,
                                String mailTitle,
                                String mailContent) throws Exception {
        Properties props = new Properties(); 

        props.put("mail.smtp.host", "smtp.sina.com.cn");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "25");
        Session session = Session.getInstance(props);


        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromMail));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(toMail));
        message.setSubject(mailTitle);
        message.setText(mailContent); 


        Transport transport = session.getTransport();
        transport.connect(user, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    public static void sendToDb()throws Exception {
        Data testDb = new Data();
        ArrayList<emailAr> emailData = testDb.Data();
        sendEm testSend = new sendEm();
        for (int i = 0; i < emailData.size(); i++) {
            testSend.sendMail("15271901633@sina.cn", "15271901633@sina.cn", "123456+-:",
                    emailData.get(i).address,
                    Text);
            System.out.printf("Having sent to %s successfully!", emailData.get(i).name);
            System.out.println();
        }
    }
}
